package club.hicode.daydayup.readball;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.RandomUtil;
import org.junit.Test;

import javax.sound.midi.Soundbank;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
 * 红包测试
 *
 * @author Liu Chunfu
 * @date 2018-08-06 下午4:57
 **/
public class ReadTest {
    @Test
    public void test1() {
        List<Double> countList = new ArrayList<>(10);
        double total = 100;
        int fen = 50;
        for (int i = fen; i > 0; i--) {
            if (i == 1) {
                countList.add(getTwoDecimal(total));
                break;
            }
            //随机分数
            Double v = RandomUtil.randomDouble((1.0 / fen) * 1, (1.0 / fen) * 3);
            double cur = v * total;
            cur = getTwoDecimal(cur);
            if (cur < 0.01) {
                cur = 0.10;
            }
            total = total - cur;
            countList.add(cur);
        }

        Collections.shuffle(countList);

        countList.forEach(System.out::println);

        Optional<Double> reduce = countList.stream().reduce(Double::sum);
        System.out.println("=====");
        System.out.println(reduce.get());
    }

    /**
     * 将数据保留两位小数
     */
    private double getTwoDecimal(double num) {
        DecimalFormat dFormat = new DecimalFormat("#.00");
        String yearString = dFormat.format(num);
        Double temp = Double.valueOf(yearString);
        return temp;
    }

    @Test
    public void randomDouble() {
        for (int i = 0; i < 100; i++) {
            Double v = RandomUtil.randomDouble(0.1, 0.3);
            System.out.println(v);
        }

    }

}
